/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */
define(['N/search', 'N/record', 'N/runtime'],
    function(search, record, runtime) {

        function onAction(context) {
            log.debug({title:'text',details:"222"});
            var lang = runtime.getCurrentUser().getPreference('LANGUAGE');
            if (lang == 'zh_CN' || lang == 'zh_TW') {
                var dtl = "\r\n没有面单，请确认操作;";
                var dmt = "\r\n没有母单号，请确认操作;";
                var dsp = "\r\n没有物流商物流方式，无法取消面单！"
                var dmc = "没有找到该物流商匹配的api客户端，请联系管理员;\r\n";
            }else {
                var dtl = "\r\nthe order hasnt tracking label , please confirm your action;";
                var dmt = "\r\nthe order hasnt main tracking number , please confirm your action;";
                var dsp = "\r\nthe order hasnt shipmethod , can not cancel the tracking";
                var dmc = "Carrier doesnt matched api client,please contact the administrator;\r\n";
            }
            var errorMSG = '';


            var rec = context.newRecord;
            var recId = context.newRecord.id;
            var errorMsg = rec.getText("custrecord_platform_so_error_msg");

            for(var i =0 ;i<errorMsg.length;i++){
                log.debug({title:i,details:errorMsg.charCodeAt(i)});
            }

            var soStatus = rec.getValue('custrecord_platform_so_status');
            if(soStatus!=3){
                throw "非错误订单";
            }
            try{

                record.submitFields({
                    type: 'customrecord_platform_so_source',
                    id: recId,
                    values: {
                        'custrecord_platform_so_error_msg':null,
                        'custrecord_platform_so_status':1
                    }
                });

                try{
                    log.audit({title:"开始处理同类型错误的订单",details:errorMsg});
                    search.create({
                        type:'customrecord_platform_so_source',
                        filters:[
                            ['custrecord_platform_so_error_msg','is',errorMsg]
                            //,'AND', ['custrecord_platform_so_status','is',3]
                        ]
                    }).run().each(function(ctx){
                        record.submitFields({
                            type: 'customrecord_platform_so_source',
                            id: ctx.id,
                            values: {
                                'custrecord_platform_so_error_msg':null,
                                'custrecord_platform_so_status':1
                            }
                        });
                        log.debug({title:"处理同类型错误的订单",details:ctx.id});
                        return true;
                    });
                }catch (e){throw e;}
            }catch (e){
                log.debug({title:'error',details:JSON.stringify(e)});
            }
        }

        function updateSOSource(sourceId,order){
            try{
                record.submitFields({
                    type: 'customrecord_platform_so_source',
                    id: sourceId,
                    values: {
                        'custrecord_platform_so_context':JSON.stringify(order),
                        'custrecord_platform_so_date':new Date(order.purchaseTime)
                    }
                });
            }catch (e){  }
        }


        return {
            onAction: onAction
        };
    });